package com.itxx.keyCase;

import java.io.*;
import java.util.Comparator;
import java.util.TreeSet;

/*
在当前模块新建一个文件number.txt,文件中有以下内容:
3,5,4,7,12,9,10,1
要求将数据读取之后重新排序后存储到number.txt中.

*/
public class Test1_2 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("day12\\number2.txt")));
        bw.write("3,5,4,7,12,9,10,1");
        bw.flush();
        bw.newLine();

        BufferedReader br = new BufferedReader(new FileReader(new File("day12\\number2.txt")));
        String s = br.readLine();
        String[] split = s.split(",");

        TreeSet<Integer> list = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {
                return integer - t1;
            }
        });

        for (int i = 0; i < split.length; i++) {
            list.add(Integer.parseInt(split[i]));
        }
        for (Integer integer : list) {
            String ss = integer.toString();
            if (integer == list.last()) {
                bw.write(ss);
            } else {
                bw.write(ss + ",");
            }
        }
        bw.flush();
        //释放资源
        bw.close();
        br.close();
    }
}
